package com.zyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyf.enums.RefundStatusEnum;
import com.zyf.mapper.RefundInfoMapper;
import com.zyf.model.order.PaymentInfo;
import com.zyf.model.order.RefundInfo;
import com.zyf.service.RefundInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;

/** 退款记录 业务层实现类
 * @author zyf
 * @date 2021年06月03日 18:36
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    /**
     * 保存退款记录
     * @author zyf
     * @date 2021/6/3 18:38
     * @param paymentInfo 支付记录实体类
     * @return com.zyf.model.order.RefundInfo
     */
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",paymentInfo.getOrderId())
                .eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);
        if (refundInfo != null){
            return refundInfo;
        }
        //添加记录
        refundInfo = new RefundInfo();
        refundInfo.setOrderId(paymentInfo.getOrderId())
                .setPaymentType(paymentInfo.getPaymentType())
                .setOutTradeNo(paymentInfo.getOutTradeNo())
                .setRefundStatus(RefundStatusEnum.UNREFUND.getStatus())
                .setSubject(paymentInfo.getSubject())
                .setTotalAmount(paymentInfo.getTotalAmount())
                .setCreateTime(new Date());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
